﻿
using APIResult = System.Collections.Generic.List<System.Tuple<string, string>>;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System;
using System.ComponentModel;
using System.Threading;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Windows.Forms;
using Microsoft.Office.Interop.Outlook;
using System.Drawing;

namespace FogOutlook.Plugins.Classes {
	public abstract class BugTrackerBase {
		protected string url;
		protected Icon icon;
		public string senderAddress = "";
		public bool ShowConfirmation { get; set; }

		// For some reason MailItem.Sender is invalid in all but the main DLL...so get the address there and pass to here
		public void Initialize(string url, string username, string password, BackgroundWorker bw) {
			this.url = url;  // Set that API URL all connections will be made against
			//this.senderAddress = senderAddress;
			Login(username, password);
			bw.ReportProgress(20);
			CacheData(bw);
		}

		public override string ToString() { return GetName(); }
		protected abstract string GetName();
		protected abstract void Login(string username, string password);
		protected abstract void CacheData(BackgroundWorker bw);
		public virtual void OpenBugReport(MailItem mi, string senderAddress) { this.senderAddress = String.IsNullOrEmpty(senderAddress) ? "" : senderAddress; }
		public virtual void DirectReport(string project, MailItem mi, string senderAddress, bool incatt) { this.senderAddress = String.IsNullOrEmpty(senderAddress) ? "" : senderAddress; }
		public virtual void ReportOnSite(string url, MailItem mi, string senderAddress) { this.senderAddress = String.IsNullOrEmpty(senderAddress) ? "" : senderAddress; }
		public virtual string[] GetProjects() { return null; }

		public void SetIcon(Icon icon) { this.icon = icon; }

		public string GetMIMEType(string filepath) {
			RegistryPermission regPerm = new RegistryPermission(RegistryPermissionAccess.Read, "\\\\HKEY_CLASSES_ROOT");
			FileInfo fi = new FileInfo(filepath);
			RegistryKey classesRoot = Registry.ClassesRoot;
			string dotExt = fi.Extension.ToLower();
			RegistryKey typeKey = classesRoot.OpenSubKey("MIME\\Database\\Content Type");

			foreach(string keyname in typeKey.GetSubKeyNames()) {
				RegistryKey curKey = classesRoot.OpenSubKey("MIME\\Database\\Content Type\\" + keyname);
				if(curKey.GetValue("Extension") != null && curKey.GetValue("Extension").ToString().ToLower() == dotExt)
					return keyname;
			}
			return "";
		}
	}

	public static class PluginLoader {
		public static Dictionary<string, BugTrackerBase> Plugins = new Dictionary<string, BugTrackerBase>();
		public static void Load(Assembly main) {
			Uri uri = new Uri(main.CodeBase);
			string dir = Path.GetDirectoryName(uri.LocalPath);
			string path = Path.Combine(dir, "Plugins");

            string[] plugins;
            try {
                plugins = Directory.GetFiles(path, "*.dll");
            } catch(DirectoryNotFoundException) { System.Diagnostics.Debug.WriteLine("No Plugins folder found..."); return; }
			foreach(string Filename in plugins) {
				Assembly Asm = Assembly.LoadFile(Filename);
				foreach(Type AsmType in Asm.GetTypes()) {
					if(AsmType.BaseType != null && AsmType.BaseType.Name == "BugTrackerBase") {
						BugTrackerBase Plugin = (BugTrackerBase)Activator.CreateInstance(AsmType);
						Plugins.Add(Plugin.ToString(), Plugin);
					}
				}
			}
		}
	}
}
